package com.beilang.hotline.knowledge.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beilang.base.core.dao.PaginationBaseDAO;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.util.StringUtil;
import com.beilang.hotline.knowledge.model.KnowledgeModel;
import com.beilang.hotline.knowledge.to.SearchTO;

@SuppressWarnings("unchecked")
public class KnowledgeDAO extends PaginationBaseDAO {
	public KnowledgeModel createKnowledge(KnowledgeModel knowledgeModel) {
        knowledgeModel.setDeleteInd("N");
        return (KnowledgeModel)this.save(knowledgeModel);
	}

	public KnowledgeModel saveKnowledge(KnowledgeModel knowledgeModel) {
		return (KnowledgeModel)this.save(knowledgeModel);
	}

	public void deleteKnowledge(Long knowledgeId){
        KnowledgeModel knowledge= this.getKnowledge(knowledgeId);
        knowledge.setDeleteInd("Y");
        this.save(knowledge);
	}

	public KnowledgeModel getKnowledge(Long knowledgeId){
		return (KnowledgeModel)this.get(KnowledgeModel.class, knowledgeId);
	}

	public Integer searchCount(SearchTO searchTO) {
        Map map = new HashMap();
        if(searchTO.getSysId()!=null){
        	map.put("sysId", searchTO.getSysId());
        }
        String knowledgeName=StringUtil.convertEmpty2Null(searchTO.getKnowledgeName());
        if(knowledgeName!=null)knowledgeName="%"+knowledgeName+"%";
        map.put("knowledgeName", knowledgeName);
        String knowledgeAnswer=StringUtil.convertEmpty2Null(searchTO.getKnowledgeAnswer());
        if(knowledgeAnswer!=null)knowledgeAnswer="%"+knowledgeAnswer+"%";
        map.put("knowledgeAnswer", knowledgeAnswer);
        return (Integer) getSqlMapClientTemplate().queryForObject("searchKnowledgeCount", map);
    }

    public List searchList(PaginationHelper helper, SearchTO searchTO) {
        Map map = new HashMap();
        if(searchTO.getSysId()!=null){
        	map.put("sysId", searchTO.getSysId());
        }
        String knowledgeName=StringUtil.convertEmpty2Null(searchTO.getKnowledgeName());
        if(knowledgeName!=null)knowledgeName="%"+knowledgeName+"%";
        map.put("knowledgeName", knowledgeName);
        String knowledgeAnswer=StringUtil.convertEmpty2Null(searchTO.getKnowledgeAnswer());
        if(knowledgeAnswer!=null)knowledgeAnswer="%"+knowledgeAnswer+"%";
        map.put("knowledgeAnswer", knowledgeAnswer);
        map = constructPaginationMap(helper, map);
        return getSqlMapClientTemplate().queryForList("searchKnowledgeList", map);
    }

    public List searchList(SearchTO searchTO) {
        Map map = new HashMap();
        if(searchTO.getSysId()!=null){
        	map.put("sysId", searchTO.getSysId());
        }
        String knowledgeName=StringUtil.convertEmpty2Null(searchTO.getKnowledgeName());
        if(knowledgeName!=null)knowledgeName="%"+knowledgeName+"%";
        map.put("knowledgeName", knowledgeName);
        String knowledgeAnswer=StringUtil.convertEmpty2Null(searchTO.getKnowledgeAnswer());
        if(knowledgeAnswer!=null)knowledgeAnswer="%"+knowledgeAnswer+"%";
        map.put("knowledgeAnswer", knowledgeAnswer);
        return getSqlMapClientTemplate().queryForList("searchKnowledgeAllList", map);
    }

    protected  String mapSortField(String dataField){
    	if(StringUtil.isNullOrBlank(dataField))return dataField;
    	if(dataField.equalsIgnoreCase("sysName")) return "system_name";
    	if(dataField.equalsIgnoreCase("knowledgeName")) return "knowledge_name";
    	if(dataField.equalsIgnoreCase("knowledgeAnswer")) return "knowledge_answer";
        if(dataField.equalsIgnoreCase("createId")) return "create_id";
        return dataField;
    }

    public void deleteAllSysKnowledges(Long sysId){
        Map map = new HashMap();
        map.put("sysId", sysId);
        getSqlMapClientTemplate().update("deleteAllSysKnowledges",map);
    }

}
